// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    password: '',
    openid: ''
  },
  // 获取用户输入的用户名
  usernameInput: function (e) {
    this.setData({
      username: e.detail.value
    })

  },
  //校验账号和密码
  userAlert() {
    var username = this.data.username;
    if (username === '' || username === null) {
      wx.showToast({
        title: '账号不能为空!',
        icon: 'loading',
        mask: false
      })
    } else {
      var $reg = /^[a-zA-Z0-9_-]{4,16}$/;
      if (!$reg.test(username)) {
        wx.showToast({
          title: '最少4位字母和数字',
          icon: 'none',
          mask: false
        })
      } else {
        wx.showToast({
          title: '账号正确',
          icon: 'success',
          mask: false
        })
      }
    }
  },
  psdAlert() {
    var password = this.data.password;
    if (password === '' || password === null) {
      wx.showToast({
        title: '密码不能为空!',
        icon: 'loading',
        mask: false
      })
    }else {
      var $reg = /^[a-z0-9_-]{6,18}$/;
      if (!$reg.test(password)) {
        wx.showToast({
          title: '最少6位字母和数字',
          icon: 'none',
          mask: false
        })
      } else {
        wx.showToast({
          title: '密码格式正确',
          icon: 'success',
          mask: false
        })
      }
    }
  },
  // 获取用户输入的密码
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  submitHandle: function (e) {
    let that = this;
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        that.setData({
          openid: res.data
        })
      }
    });

    wx.request({
      url: 'https://www.2edge.cn/rest/wx/binding',
      method: 'POST',
      data: {
        "openid": this.data.openid,
        "username": this.data.username,
        "password": this.data.password,
        "avatarurl": "",
        "nickname": "",
        "gender": "",
        "country": "",
        "province": "",
        "city": ""
      },
      success: function (res) {
        console.log(res);
        if (res.data.status) {
          wx.setStorage({
            key: "token",
            data: res.data.data.token
          })

          wx.redirectTo({ url: '/pages/home/home' })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


})